package com.note.feng.juc.masterWorker;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * @author yuhf
 */
public class MasterWorkerTest {
    //简单任务
    static class SimpleTask extends Task<Integer>{
        @Override
        protected Integer doExecute(){
            System.out.println("task "+ getId() +" is done ");
            return getId();
        }
    }
    public static void main(String[] args){
        //创建Master，包含4个Worker，并启动Master的执行线程
        Master<SimpleTask, Integer> master = new Master<>(4);
        //定期向Master提交任务
        ScheduledExecutorService executor = Executors.newScheduledThreadPool(2);
        executor.scheduleAtFixedRate(() -> master.submit(new SimpleTask()),
                0, 2, TimeUnit.SECONDS);
        //定期从Master提取结果
        executor.scheduleAtFixedRate(() -> master.printResult(),0, 5, TimeUnit.SECONDS);
    }
}
